-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bottleneck
function
#145
base: main
Are you sure you want to change the base?
Conversation
What do you think about 'ratelimit' name to make it more explicit? |
Not against the idea. The name Claude Sonnet 3.5 seemed to like both names 😄 |
// every finished call. | ||
const next = () => queue.length && run(queue.shift()!) | ||
|
||
const bottled: BottledFunction<TArgs, any> = (...args) => run(args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can create a QueueItem
here, push it in the queue and call next()
. That should help us to simplify run
function to run(input: QueueItem)
.
Something like this:
const bottled: BottledFunction<TArgs, any> = (...args) => {
const { promise, resolve, reject } = Promise.withResolvers();
const item = { args, resolve, reject };
queue.push(item);
next();
return promise;
}
Probably we need to create our internal withResolvers
util since that API is very new.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we need to create our internal
withResolvers
util since that API is very new.
Would you be interested in contributing that?
You can do this in terminal to create the files:
pnpm add-function async/withResolvers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on the simplification, even if it means a little more GC work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a PR #148
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW @aleclarson add-function
doesn't put an export into src/mod.ts
. (also, we can use https://github.com/google/zx instead of sh, but maybe it's overengineering)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add-function
doesn't put an export intosrc/mod.ts
Yeah I hope to fix that eventually 😅
The add-function
script should have warned you though. Did it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, no 🤔
$ pnpm add-function async/withResolvers
> radashi@12.1.0 add-function /home/vladimir/code/radashi
> bash ./scripts/add-function.sh "async/withResolvers"
Enter a description for withResolvers:
Ponyfill for Promise.withResolvers()
I think The name "bottleneck" implies that every call will eventually execute, like emptying water from a bottle in real life. That seems to imply it wouldn't be possible to bloat But perhaps discoverability is most important here. 🤔 |
536809a
to
47a03ef
Compare
87b84da
to
1d5ab39
Compare
1d5ab39
to
39c57ff
Compare
Tip
The owner of this PR can publish a preview release by commenting
/publish
in this PR. Afterwards, anyone can try it out by runningpnpm add radashi@pr<PR_NUMBER>
.Summary
Limit how many times a function can be called per time interval (in milliseconds). Optionally set a concurrency limit.
Most useful for rate limiting.
Related issue, if any:
lodash/lodash#2102 (cc @not-an-aardvark from there)
For any code change,
Does this PR introduce a breaking change?
No
Bundle impact
src/curry/bottleneck.ts
Footnotes
Function size includes the
import
dependencies of the function. ↩